using HYBY.CommonLibrary.Configuration;
/*-----------------------------------------------------------------------------------------------
// Copyright (C) yangrongfeng 
//
// 文件名：        UrlHelperExtension.cs
// 文件功能描述：   UrlHelper扩展
//
// 
// 创建标识：       庞永飞  2016-05-11
//
// 修改标识：庞永飞
// 修改描述：新增文件上传和视频上传URL生成功能
//
// 备注：	
// 				
//-----------------------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HYBY.WebFrameWork.Mvc {    

    public static class UrlHelperExtension {
        //图片上传服务器列表
        private static List<String> ImageServers;
        private static List<String> FileServers;
        private static List<String> VideoServers;

        //private  const String SYSTEM_NAME = "Engine";
        private static String Image_UploadPath;
        private static String File_UploadPath;
        private static String Video_UploadPath;

        #region 初始化

        static UrlHelperExtension() {
            Initialize();
        }

        /// <summary>
        /// 初始化，从配置文件中读取上传服务器列表和系统编码。
        /// 图片服务器按照{系统编号}/{业务单号}/{图片规格}/{图片}的方式保存上传的图片文件
        /// </summary>
        private static void Initialize() {
            String filePath = HttpContext.Current.Server.MapPath(@"/config/UploadServer.config");
            var UploadServerConf = (UploadServerConfiguration)ConfigurationHelper.GetSection("UploadServerSetting",filePath);
            //初始化图片上传服务列表
            ImageServers = new List<string>();
            Image_UploadPath = UploadServerConf.ImageServers.UploadPath;
            foreach (ServerDescriptor server in UploadServerConf.ImageServers) {
                ImageServers.Add(server.ServerURL);            
            }
            //初始化文件上传服务列表
            FileServers = new List<string>();
            File_UploadPath = UploadServerConf.FileServers.UploadPath;
            foreach (ServerDescriptor server in UploadServerConf.FileServers) {
                FileServers.Add(server.ServerURL);
            }
            //初始化视频上传服务列表
            VideoServers = new List<string>();
            Video_UploadPath = UploadServerConf.VideoServers.UploadPath;
            foreach (ServerDescriptor server in UploadServerConf.VideoServers) {
                VideoServers.Add(server.ServerURL);
            }            
        }

        #endregion

        #region 图片上传

        /// <summary>
        /// 根据跟定的业务单号生成图片上传的URL地址
        /// </summary>
        /// <param name="helper">UrlHelper</param>
        /// <param name="billNo">业务单号</param>
        /// <returns>图片上传URL地址</returns>
        public static Uri ImageUpload(this UrlHelper helper, String billNo) {
            return ImageUpload(helper, billNo,false, false, null);
        }

        /// <summary>
        /// 根据给定的业务单号和图片规格生成图片上传的URL地址
        /// </summary>
        /// <param name="helper">UrlHelper</param>
        /// <param name="billNo">业务单号</param>
        /// <param name="imageSpecifications">图片规格要求</param>
        /// <returns>图片上传URL地址</returns>
        public static Uri ImageUpload(this UrlHelper helper, String billNo, String[] imageSpecifications) {
            return ImageUpload(helper, billNo, false, false, imageSpecifications);
        }

        /// <summary>
        /// 根据给定的业务单号和添加文本水印生成图片上传的URL地址
        /// </summary>
        /// <param name="helper">UrlHelper</param>
        /// <param name="billNo">业务单号</param>
        /// <param name="isAddTextWarterMark">是否添加文本水印</param>
        /// <returns>图片上传URL地址</returns>
        public static Uri ImageUpload(this UrlHelper helper, String billNo, bool isAddTextWarterMark) {
            return ImageUpload(helper, billNo, isAddTextWarterMark, false, null);
        }

        /// <summary>
        /// 根据给定的业务单号和添加图片水印生成图片上传的URL地址
        /// </summary>
        /// <param name="helper">UrlHelper</param>
        /// <param name="isAddImageWaterMark">是否添加图片水印</param>
        /// <param name="billNo">业务单号</param>
        /// <returns>图片上传URL地址</returns>
        public static Uri ImageUpload(this UrlHelper helper, bool isAddImageWaterMark, String billNo) {
            return ImageUpload(helper, billNo, false, isAddImageWaterMark, null);
        }


        /// <summary>
        /// 根据给定的业务单号、添加文本水印、添加图片水印和图片规格生成图片上传的URL地址
        /// </summary>
        /// <param name="helper">UrlHelper</param>
        /// <param name="billNo">业务单号</param>
        /// <param name="isAddTextWarterMark">是否添加文本水印</param>
        /// <param name="isAddImageWaterMark">是否添加图片水印</param>
        /// <param name="imageSpecifications">图片规格</param>
        /// <returns>图片上传URL地址</returns>
        public static Uri ImageUpload(this UrlHelper helper, String billNo, bool isAddTextWarterMark,
            bool isAddImageWaterMark, String[] imageSpecifications) {
            if (imageSpecifications == null) imageSpecifications = new String[] { "" };
            Random Rand = new Random();
            int Index = Rand.Next(ImageServers.Count);
            String ImageServer = ImageServers[Index];
            String SavePath = Image_UploadPath + "/" + billNo + "/";
            if (String.IsNullOrWhiteSpace(billNo)) SavePath = Image_UploadPath + "/";
            String Specifications = "";
            foreach (String s in imageSpecifications) {
                Specifications += s + ";";
            }
            String url =String.Format(@"{0}?action={1}&isAddTextWarterMark={2}&isAddImageWaterMark{3}&savePath={4}&imageSpecifications={5}",
                                   ImageServer,
                                   "uploadimage",                                     
                                   isAddTextWarterMark,
                                   isAddImageWaterMark,
                                   SavePath,
                                   Specifications);
            Uri uri = new Uri(url);
            return uri;
        }

        #endregion

        #region 文件上传

        /// <summary>
        /// 根据跟定的业务单号生成文件上传的URL地址
        /// </summary>
        /// <param name="helper">UrlHelper</param>
        /// <param name="billNo">业务单号</param>
        /// <returns>文件上传URL地址</returns>
        public static Uri FileUpload(this UrlHelper helper, String billNo) {
            Random Rand = new Random();
            int Index = Rand.Next(FileServers.Count);
            String FileUploadServer = FileServers[Index];
            String SavePath = File_UploadPath + "/" + billNo + "/";
            if (String.IsNullOrWhiteSpace(billNo)) SavePath = File_UploadPath + "/";           
            String url = String.Format(@"{0}?action={1}&savePath={4}",
                                   FileUploadServer,
                                   "uploadimage",                                   
                                   SavePath);
            Uri uri = new Uri(url);
            return uri;
        }

        #endregion

        #region  视频上传

        /// <summary>
        /// 根据跟定的业务单号生成视频上传的URL地址
        /// </summary>
        /// <param name="helper">UrlHelper</param>
        /// <param name="billNo">业务单号</param>
        /// <returns>视频上传URL地址</returns>
        public static Uri VideoUpload(this UrlHelper helper, String billNo) {
            Random Rand = new Random();
            int Index = Rand.Next(VideoServers.Count);
            String VideoUploadServer = VideoServers[Index];
            String SavePath = Video_UploadPath + "/" + billNo + "/";
            if (String.IsNullOrWhiteSpace(billNo)) SavePath = Video_UploadPath + "/";
            String url = String.Format(@"{0}?action={1}&savePath={4}",
                                   VideoUploadServer,
                                   "uploadimage",
                                   SavePath);
            Uri uri = new Uri(url);
            return uri;
        }

        #endregion

    }   
}